/*
 * Copyright (C) 2011 CrystalRange
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.crystalrange.enterprise.web;

import java.util.Set;
import org.crystalrange.BaseController;
import org.crystalrange.Message;
import org.crystalrange.enterprise.model.CompanyCode;
import org.crystalrange.enterprise.model.Plant;
import org.crystalrange.enterprise.model.PurchaseOrganization;
import org.crystalrange.enterprise.service.PlantService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.util.Clients;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Window;

public class PlantController extends BaseController {

    protected PlantService plantService;
    protected Plant plant;
    protected Listbox listboxPurchaseOrganization;

    public PlantService getPlantService() {
        return this.plantService;
    }

    public void setPlantService(PlantService plantService) {
        this.plantService = plantService;
    }

    public Plant getPlant() {
        return this.plant;
    }

    public void setPlant(Plant plant) {
        this.plant = plant;
    }

    @Override
    public void doAfterCompose(Component component) throws Exception {
        super.doAfterCompose(component);

        if (this.execution.getParameter("Id") != null) {
            this.plant = this.plantService.find(
                    new Long(this.execution.getParameter("Id")));
            this.labelTitle.setValue("Edit Plant");
        } else {
            this.plant = this.plantService.create(
                    SecurityContextHolder.getContext().getAuthentication().getName());
            this.labelTitle.setValue("Create Plant");
        }

        component.setAttribute("controller", this, true);
    }

    public void onClick$menuitemPurchaseOrganizationAdd(Event event) throws Exception {
        this.purchaseOrganizationAdd();
    }

    public void onClick$menuitemPurchaseOrganizationRemove(Event event) throws Exception {
        this.purchaseOrganizationRemove();
    }

    public void onClick$toolbarbuttonPurchaseOrganizationAdd(Event event) throws Exception {
        this.purchaseOrganizationAdd();
    }

    public void onClick$toolbarbuttonPurchaseOrganizationRemove(Event event) throws Exception {
        this.purchaseOrganizationRemove();
    }

    public void onClick$menuitemPlantSave(Event event) throws Exception {
        this.plantSave();
    }

    public void onClick$toolbarbuttonPlantSave(Event event) throws Exception {
        this.plantSave();
    }

    public void onOpen$bandboxCompanyCode(Event event) throws Exception {
        Window window = (Window) this.execution.createComponents("~./zul/html/companyCodeSelect.zul", null, null);
        window.setParent(self);
        window.doModal();

        if (this.execution.getAttribute("result") == "ok") {
            CompanyCode selectedCompanyCode = (CompanyCode) this.execution.getAttribute("selectedCompanyCode");
            this.plant.setCompanyCode(selectedCompanyCode);
        }

        this.binder.loadAll();
    }

    private void purchaseOrganizationAdd() throws Exception {
        Window window = (Window) this.execution.createComponents("~./zul/html/purchaseOrganizationSelect.zul", null, null);
        window.setParent(self);
        window.doModal();

        if (this.execution.getAttribute("result") == "ok") {
            PurchaseOrganization selectedPurchaseOrganization = (PurchaseOrganization) this.execution.getAttribute("selectedPurchaseOrganization");
            this.plant.getPurchaseOrganizations().add(selectedPurchaseOrganization);
        }

        this.binder.loadComponent(this.listboxPurchaseOrganization);
    }

    private void purchaseOrganizationRemove() throws Exception {
        Set<Listitem> selectedItems = this.listboxPurchaseOrganization.getSelectedItems();
        for (Listitem selectedItem : selectedItems) {
            PurchaseOrganization selectedPurchaseOrganization = (PurchaseOrganization) selectedItem.getValue();
            this.plant.getPurchaseOrganizations().remove(selectedPurchaseOrganization);
        }

        this.binder.loadComponent(this.listboxPurchaseOrganization);
    }

    private void plantSave() throws Exception {
        this.plantService.save(this.plant,
                SecurityContextHolder.getContext().getAuthentication().getName());
        this.sessionScope.put("CRYSTALRANGE_MESSAGE", new Message("Saved successfully!"));
        Clients.evalJavaScript("window.history.back();");
    }
}
